let bool = false;
let initId = null;

function calcTree(tree = []) {
    let arr = [];
    tree.length > 0 && tree.forEach(item => {
        const {validNums,needType, attachmentName, id, attachmentClass, children, itemNum, maxFileNums, minFileNums, sortOrder, needNums, busiNode, parentId} = item;
        let obj = {
            title: attachmentName,
            needType,
            attachmentName,
            id,
            attachmentClass,
            itemNum,
            maxFileNums,
            minFileNums,
            sortOrder,
            needNums,
            busiNode,
            parentId,
            validNums,
            expand: false,
            selected: false
        };
        if (attachmentClass === 1) {
            obj.expand = true;
        } else if (attachmentClass === 2) {
            if (children && Array.isArray(children)) {
                let index = children.findIndex(e => e.needNums > 0)
                if (index >= 0) {
                    obj.expand = true;
                }
            }
        } else if (attachmentClass === 3 && bool) {
            obj.selected = true;
            bool = false;
        }
        if(initId === id) {
            obj.selected = true;
        }
        if (itemNum > 1 && needNums >= 1 && attachmentClass == 2 && itemNum > needNums) {
            obj.title = obj.title + "(" + itemNum + "选" + needNums + ")";
        }
        obj.children = calcTree(children);

        arr.push(obj);
    });
    return arr;
}

function getUploadTree(tree = [],initNode) {
    if(initNode) {
        initId = initNode.id
    }else {
        bool = true
    }
    return calcTree(tree)
}

export default getUploadTree;
